require "Util.StringUtil"

module("StringUtilTest", package.seeall, lunit.testcase)

function test_split()
	local text, length, parts

	text = "part1,part2"
	assert_error_match("Invalid delimiter", function() StringUtil.split(text, "") end)

	length, parts = StringUtil.split(text, ",")
	assert_equal(2, length)
	assert_equal("part1", parts[1])
	assert_equal("part2", parts[2])

	text = ","
	length, parts = StringUtil.split(text, ",")
	assert_equal(0, length)

	text = ",,,,,"
	length, parts = StringUtil.split(text, ",")
	assert_equal(0, length)

	text = "part1,,,,,"
	length, parts = StringUtil.split(text, ",")
	assert_equal(1, length)
	assert_equal("part1", parts[1])

	text = ",,,,,part1,,,,,"
	length, parts = StringUtil.split(text, ",")
	assert_equal(1, length)
	assert_equal("part1", parts[1])

	text = "part1,,,,,part2,,,,,"
	length, parts = StringUtil.split(text, ",")
	assert_equal(2, length)
	assert_equal("part1", parts[1])
	assert_equal("part2", parts[2])

	text = "part1,,,,,part2,,,,,part3"
	length, parts = StringUtil.split(text, ",")
	assert_equal(3, length)
	assert_equal("part1", parts[1])
	assert_equal("part2", parts[2])
	assert_equal("part3", parts[3])

	text = " part1 , part2 "
	length, parts = StringUtil.split(text, ",")
	assert_equal(2, length)
	assert_equal(" part1 ", parts[1])
	assert_equal(" part2 ", parts[2])

	length, parts = StringUtil.split(text, ",", true)
	assert_equal(2, length)
	assert_equal("part1", parts[1])
	assert_equal("part2", parts[2])
end

function test_trim()
	local text, space

	space = "	 "

	text = space .. space
	assert_equal("", StringUtil.trim(text))

	text = space .. "text1, text2" .. space
	assert_equal("text1, text2", StringUtil.trim(text))
end
